/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.close.basic.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.hopes.bizz.close.basic.entity.ClosedControlAreaEntity;
import com.hopes.bizz.close.basic.dto.ClosedControlAreaExcelDTO;
import com.hopes.bizz.close.basic.vo.ClosedControlAreaExcelVO;
import com.hopes.bizz.close.basic.mapper.ClosedControlAreaMapper;
import com.hopes.bizz.close.basic.service.ClosedControlAreaService;
import com.hopes.bizz.close.common.constant.enums.ParkCloseSysDictEnum;
import com.hopes.bizz.common.core.validate.ValidatorUtils;
import com.hopes.boss.admin.api.resolver.DictResolver;
import com.hopes.boss.admin.api.resolver.enums.AdminDictEnum;
import com.hopes.boss.admin.api.resolver.enums.SysDictEnum;
import com.hopes.boss.common.core.constant.CommonConstants;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
/**
 * 管控区域
 *
 * @author pig
 * @date 2023-10-20 09:03:39
 */
@Service
public class ClosedControlAreaServiceImpl extends ServiceImpl<ClosedControlAreaMapper, ClosedControlAreaEntity> implements ClosedControlAreaService {

    @Override
    public boolean save(ClosedControlAreaEntity entity) {
        entity.setLocatedFlag(CommonConstants.DEFAULT_FALSE_FLAG);
        if (StrUtil.isNotBlank(entity.getControlArea())) {
            entity.setLocatedFlag(CommonConstants.DEFAULT_TREE_FLAG);
        }
        return SqlHelper.retBool(getBaseMapper().insert(entity));
    }

    @Override
    public boolean updateById(ClosedControlAreaEntity entity) {
        entity.setLocatedFlag(CommonConstants.DEFAULT_FALSE_FLAG);
        if (StrUtil.isNotBlank(entity.getControlArea())) {
            entity.setLocatedFlag(CommonConstants.DEFAULT_TRUE_FLAG);
        }
        return SqlHelper.retBool(getBaseMapper().updateById(entity));
    }

    @Override
    public ValidateImportBO<ClosedControlAreaExcelDTO> validateImport(List<ClosedControlAreaExcelDTO> excelDTOList) {
        ValidateImportBO<ClosedControlAreaExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<ClosedControlAreaExcelDTO> saveList = new ArrayList<>();
        int successNum = 0;
        int failedNum = 0;

        for (ClosedControlAreaExcelDTO closedControlAreaExcelDTO : excelDTOList) {
            final String validateRes = ValidatorUtils.importValidateEntity(closedControlAreaExcelDTO);
            if (StrUtil.isEmpty(validateRes)) {
                successNum++;
                saveList.add(closedControlAreaExcelDTO);
            } else {
                failedNum++;
                errorMessageList.add(new ErrorMessage(closedControlAreaExcelDTO.getLineNum(), new HashSet<>(){
                    private static final long serialVersionUID = 2114050778826325599L;
                    {
                        add(validateRes);
                    }
                }));
            }
        }

        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public ClosedControlAreaEntity importDtoToEntity(ClosedControlAreaExcelDTO excelDto) {
        ClosedControlAreaEntity entity= new ClosedControlAreaEntity();
        BeanUtils.copyProperties(excelDto, entity);
        if (StrUtil.isNotEmpty(excelDto.getUseFlagStr())) {
            entity.setUseFlag(DictResolver.getItemValueByLabel(SysDictEnum.yes_no_type, excelDto.getUseFlagStr()));
        }
        if (StrUtil.isNotEmpty(excelDto.getOrgIdStr())) {
            entity.setOrgId(DictResolver.getItemValueByLabel(AdminDictEnum.sys_company, excelDto.getOrgIdStr(), Long::valueOf));
        }
        if (StrUtil.isNotEmpty(excelDto.getLevelStr())) {
            entity.setLevel(DictResolver.getItemValueByLabel(ParkCloseSysDictEnum.close_area_control_level, excelDto.getLevelStr(), Integer::valueOf));
        }
        entity.setLocatedFlag(CommonConstants.DEFAULT_FALSE_FLAG);
        return entity;
    }

    @Override
    public ClosedControlAreaExcelVO exportEntityToVo(ClosedControlAreaEntity entity) {
        ClosedControlAreaExcelVO excelVO = new ClosedControlAreaExcelVO();
        BeanUtils.copyProperties(entity, excelVO);
        excelVO.setUseFlagStr(DictResolver.getItemLabelByValue(SysDictEnum.yes_no_type, entity.getUseFlag()));
        excelVO.setLocatedFlagStr(DictResolver.getItemLabelByValue(SysDictEnum.yes_no_type, entity.getLocatedFlag()));
        return excelVO;
    }
}
